/**
 * Created by mooshroom on 2017/3/23.
 */
function time(){
    var date = new Date();//获取当前日期
    var week;//获取星期
    switch (date.getDay()){
        case 1: week="Monday"; break;
        case 2: week="Tuesday"; break;
        case 3: week="Wednesday"; break;
        case 4: week="Thursday"; break;
        case 5: week="Friday"; break;
        case 6: week="Saturday"; break;
        default:week="Sunday"; break;
    }
    var month;  //获取月份
    switch (date.getMonth()+1){
        case 1: month="January"; break;
        case 2: month="february "; break;
        case 3: month="March "; break;
        case 4: month="April "; break;
        case 5: month="May "; break;
        case 6: month="June "; break;
        case 7: month="July "; break;
        case 8: month="August"; break;
        case 9: month="September"; break;
        case 10: month="October "; break;
        case 11: month="November"; break;
        case 12: month="December"; break;
    }
    var year = date.getFullYear();//获取年份
    var day = date.getDate();//获取当天的日
    if(day<10)
        day= "0"+day;
    var hours =date.getHours();//获取小时
    if(hours<10)
        hours= "0"+hours;
    var minutes =date.getMinutes();//获取分
    if(minutes<10)
        minutes= "0"+minutes;
    var day =day+" "+month+","+week;
    var H=hours;
    var M=minutes;
    document.getElementById("YY").innerHTML= year;
    document.getElementById("day").innerHTML= day;
    document.getElementById("H").innerHTML= H;
    document.getElementById("M").innerHTML= M;
}
//设置1秒调用一次show_cur_times函数
setInterval("time()",100);


define('Home', ['avalon', 'text!../../package/Home/Home.html', 'css!../../package/Home/Home.css'],
    function (avalon, html, css) {
    var vm = avalon.define({
        $id: "Home",
        ready: function (i) {
            vm.reset()
            // i 页面：0-todoList 1-doneList
            index.html=html
            //如果i不是0或者1，那么默认i为0
            var checkList = [0, 1]
            if (checkList.indexOf(i) == -1) {
                i = 0
            }

            vm.page = i

            avalon.scan()
            vm.liu()
        },
        reset: function () {
           vm.Account=cache.go("Account"),
                vm.PWD=cache.go("PWD")
        },
        PWD:"",
        Account:"",
        LoginOut:function () {
            require(['../../obj/User'],function (obj) {
                obj.logout(function (res) {
                    vm.PWD=""
                    vm.Account=""
                    goto("#!/Login/0")
                },function (err) {
                    tip.on(err)
                })
            })
        },

        page: "",
        pageToggle:function (i) {
            vm.Status=i
        },
        newList : -1,
        sakai:function (i) {
            vm.newList = i;

        },


        Editing: false,
        Focusing:true,
        xiuGai:false,
        Check:0,
        //添加
        adds:function () {
            vm.Editing=true
            vm.Focusing=false
            vm.TaskName=""
            vm.CTime=""
        },
        //取消编辑
        cancel:function () {
            vm.xiuGai=false
        },
        //添加
        TaskName:"",
        CTime:"",
        todoList:[],
        add:function () {
            var data={
                N:99999,
                TaskName:vm.TaskName,
                CTime:vm.CTime
            }
            require(['../../obj/Task'],function (obj) {
                obj.add(data,function (res) {
                    vm.todoList=res.d
                    vm.Focusing=true
                    vm.liu()
                },function (err) {
                    tip.on(err)
                })
            })

        },
        //获取列表
        liu:function () {
            var data={
                N:99999,
                p:1,
                TaskName:vm.TaskName,
                CTime:vm.CTime
            }
            $$.call({
                i:"NewCome/Task/search",
                data:data,
                success:function (res) {
                    vm.todoList=res.L
                    vm.Editing=false
                },
                error:function (err) {
                    tip.on(err)
                }
            })
        },
        //删除
        del:function (i) {
            var LID=vm.todoList[i].LID
            vm.todoList.splice(i,1)
            require(['../../obj/Task'],function (obj) {
                obj.del(LID,function (res) {
                    tip.on("删除成功")
                },function (err) {
                    tip.on(err)
                })
            })

        },
        //编辑
        mm:true,
        editToggle:function (i) {
            vm.xiuGai=true
            vm.TaskNames=vm.todoList[i].TaskName
            vm.CTimes=vm.todoList[i].CTime
            vm.LID=vm.todoList[i].LID
        },
        //编辑保存
        TaskNames:"",
        CTimes:"",
        LID:"",
        save:function(){
            var Params={
                TaskName:vm.TaskNames,
                CTime:vm.CTimes
            }
            require(['../../obj/Task'],function (obj) {
                obj.save(vm.LID,Params,function (res) {
                    vm.xiuGai=false
                    vm.Focusing=true
                    vm.todoList=res.d
                    vm.liu()
                },function (err) {
                    tip.on(err)
                })
            })
        },
        //完成
        Status:"",/*0未完成  1完成 */
        checkToggle:function(i) {
            var LID=vm.todoList[i].LID
            var Status=vm.todoList[i].Status
            if(Status=0){
                var Params={
                    Status:0
                }
                require(['../../obj/Task'],function (obj) {
                    obj.save(LID,Params,function (res) {
                        vm.todoList=res.d
                        vm.liu()
                    },function (err) {
                        tip.on(err)
                    })
                })
            }else {
                var Params={
                    Status:1
                }
                require(['../../obj/Task'],function (obj) {
                    obj.save(LID,Params,function (res) {
                        vm.todoList=res.d
                        vm.liu()
                    },function (err) {
                        tip.on(err)
                    })
                })
            }




        }

       /* $task: {
            EndTime: "",
            TaskName: "",
            Editing: true,
            Focusing:true,
            Check:0
        },

        /!*list: [],
        
        newList : -1,
        sakai:function (i) {
            vm.newList = i;
            document.getElementById(1)
        },*!/
        //添加的方法
        /!*add:function () {
            vm.list.unshift(vm.$task)

        },*!/

        //保存的方法
        /!*save:function (i) {
            /!*var data={
                TaskName:vm.$task.TaskName,
                CTime:vm.$task.EndTime
            }
            require(['../../obj/Task'],function (obj) {
                obj.add(data,function (res) {
                    tip.on(err)
                },function (err) {
                    tip.on(err)
                })
            })*!/
            //todo 调用服务器的添加任务接口添加
            if(i>=0&&i<vm.list.length){
                vm.editToggle(i)
                vm.focusToggle(i)
            }
            //缓存本地
            vm.saveLocal()
        },*!/

        //撤销编辑
        $befor:{
            EndTime: "",
            TaskName: "",
            Editing: true,
            Focusing:true,
            Check:0
        },
        cancel:function (i) {
            avalon.mix(vm.todoList[i],vm.$befor)
            vm.editToggle(i)
            vm.focusToggle(i)
        },

        //删除
        /!*del:function (i) {
            vm.list.splice(i,1)
            vm.save()
        },*!/

        //标记完成
        checkToggle:function (i) {
            vm.todoList[i].Editing=true;
            vm.todoList[i].Focusing=true;

            vm.todoList[i].Check=!vm.todoList[i].Check

            vm.save(i)
        },

        //选中某个任务的方法
        focusToggle:function (i) {

        },
        //进入编辑状态
        editToggle:function (i) {
            //避免在进入编辑状态时有其他的块也在编辑状态，如果有，则关闭其他块的编辑状态
            for(var o=0;o<vm.todoList.length;o++){
                if(vm.todoList[o].Editing&&o!=i){
                    vm.todoList[o].Editing=false
                }
            }

            //修改编辑状态
            vm.todoList[i].Editing=!vm.todoList[i].Editing

            //如果是编辑装填的，则缓存原来的数据，以便后面撤销用
            if(vm.todoList[i].Editing){
                ForEach(vm.$befor,function (value, key) {
                    vm.$befor[key]=vm.todoList[i][key]
                })
            }
        },

        //缓存的方法
       /!* saveLocal:function () {
            var data=[]
            var $item={
                EndTime: "",
                TaskName: "",
                Check:0
            }

            vm.list.forEach(function (el) {
                var o={}
                ForEach($item,function (val, key) {
                    o[key]=el[key]
                })

                data.push(o)
            })

            var saveingData=JSON.stringify(data)
            console.log(saveingData)

            window.localStorage.setItem('task',saveingData)
        },*!/

        //提取缓存的方法
        /!*getLocal:function () {
            var list=JSON.parse(window.localStorage.getItem('task'))
            list.forEach(function (el) {
                avalon.mix(el,{
                    Editing: false,
                    Focusing:false,
                })
            })

            vm.list=list
        }*!/*/

    })
    return window[vm.$id]=vm
})